<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CAD_Doc: Assertions Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  $(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">CAD_Doc<span id="projectnumber">&#160;1.0</span>
   </div>
   <div id="projectbrief">cad synopsis</div>
  </td>
    <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <span id="MSearchSelect"                onmouseover="return searchBox.OnSearchSelectShow()"                onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
          <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('md__build_2__deps_2googletest-src_2docs_2reference_2assertions.html',''); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">Assertions Reference</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md250"></a></p>
<p>This page lists the assertion macros provided by GoogleTest for verifying code behavior. To use them, include the header <code><a class="el" href="gtest_8h_source.html">gtest/gtest.h</a></code>.</p>
<p>The majority of the macros listed below come as a pair with an <code>EXPECT_</code> variant and an <code>ASSERT_</code> variant. Upon failure, <code>EXPECT_</code> macros generate nonfatal failures and allow the current function to continue running, while <code>ASSERT_</code> macros generate fatal failures and abort the current function.</p>
<p>All assertion macros support streaming a custom failure message into them with the <code>&lt;&lt;</code> operator, for example:</p>
<div class="fragment"><div class="line">EXPECT_TRUE(my_condition) &lt;&lt; <span class="stringliteral">&quot;My condition is not true&quot;</span>;</div>
</div><!-- fragment --><p>Anything that can be streamed to an <code>ostream</code> can be streamed to an assertion macro—in particular, C strings and string objects. If a wide string (<code>wchar_t*</code>, <code>TCHAR*</code> in <code>UNICODE</code> mode on Windows, or <code>std::wstring</code>) is streamed to an assertion, it will be translated to UTF-8 when printed.</p>
<h1><a class="anchor" id="success-failure"></a>
Explicit Success and Failure</h1>
<p>The assertions in this section generate a success or failure directly instead of testing a value or expression. These are useful when control flow, rather than a Boolean expression, determines the test's success or failure, as shown by the following example:</p>
<div class="fragment"><div class="line"><span class="keywordflow">switch</span>(expression) {</div>
<div class="line">  <span class="keywordflow">case</span> 1:</div>
<div class="line">    ... some checks ...</div>
<div class="line">  <span class="keywordflow">case</span> 2:</div>
<div class="line">    ... some other checks ...</div>
<div class="line">  <span class="keywordflow">default</span>:</div>
<div class="line">    FAIL() &lt;&lt; <span class="stringliteral">&quot;We shouldn&#39;t get here.&quot;</span>;</div>
<div class="line">}</div>
</div><!-- fragment --><h2><a class="anchor" id="SUCCEED"></a>
SUCCEED</h2>
<p><code>SUCCEED()</code></p>
<p>Generates a success. This <em>does not</em> make the overall test succeed. A test is considered successful only if none of its assertions fail during its execution.</p>
<p>The <code>SUCCEED</code> assertion is purely documentary and currently doesn't generate any user-visible output. However, we may add <code>SUCCEED</code> messages to GoogleTest output in the future.</p>
<h2><a class="anchor" id="FAIL"></a>
FAIL</h2>
<p><code>FAIL()</code></p>
<p>Generates a fatal failure, which returns from the current function.</p>
<p>Can only be used in functions that return <code>void</code>. See <a href="../advanced.md#assertion-placement">Assertion Placement</a> for more information.</p>
<h2><a class="anchor" id="ADD_FAILURE"></a>
ADD_FAILURE</h2>
<p><code>ADD_FAILURE()</code></p>
<p>Generates a nonfatal failure, which allows the current function to continue running.</p>
<h2><a class="anchor" id="ADD_FAILURE_AT"></a>
ADD_FAILURE_AT</h2>
<p><code>ADD_FAILURE_AT(</code>*<code>file_path</code>*<code>,</code>*<code>line_number</code>*<code>)</code></p>
<p>Generates a nonfatal failure at the file and line number specified.</p>
<h1><a class="anchor" id="generalized"></a>
Generalized Assertion</h1>
<p>The following assertion allows <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2matchers.html">matchers</a> to be used to verify values.</p>
<h2><a class="anchor" id="EXPECT_THAT"></a>
EXPECT_THAT</h2>
<p><code>EXPECT_THAT(</code>*<code>value</code>*<code>,</code>*<code>matcher</code>*<code>)</code> \ <code>ASSERT_THAT(</code>*<code>value</code>*<code>,</code>*<code>matcher</code>*<code>)</code></p>
<p>Verifies that <em><code>value</code></em> matches the <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2matchers.html">matcher</a> <em><code>matcher</code></em>.</p>
<p>For example, the following code verifies that the string <code>value1</code> starts with <code>"Hello"</code>, <code>value2</code> matches a regular expression, and <code>value3</code> is between 5 and 10:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;gmock/gmock.h&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using </span>::testing::AllOf;</div>
<div class="line"><span class="keyword">using </span>::testing::Gt;</div>
<div class="line"><span class="keyword">using </span>::testing::Lt;</div>
<div class="line"><span class="keyword">using </span>::testing::MatchesRegex;</div>
<div class="line"><span class="keyword">using </span>::testing::StartsWith;</div>
<div class="line"> </div>
<div class="line">...</div>
<div class="line">EXPECT_THAT(value1, StartsWith(<span class="stringliteral">&quot;Hello&quot;</span>));</div>
<div class="line">EXPECT_THAT(value2, MatchesRegex(<span class="stringliteral">&quot;Line \\d+&quot;</span>));</div>
<div class="line">ASSERT_THAT(value3, AllOf(Gt(5), Lt(10)));</div>
</div><!-- fragment --><p>Matchers enable assertions of this form to read like English and generate informative failure messages. For example, if the above assertion on <code>value1</code> fails, the resulting message will be similar to the following:</p>
<div class="fragment"><div class="line">Value of: value1</div>
<div class="line">  Actual: &quot;Hi, world!&quot;</div>
<div class="line">Expected: starts with &quot;Hello&quot;</div>
</div><!-- fragment --><p>GoogleTest provides a built-in library of matchers—see the <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2matchers.html">Matchers Reference</a>. It is also possible to write your own matchers—see <a href="../gmock_cook_book.md#NewMatchers">Writing New Matchers Quickly</a>. The use of matchers makes <code>EXPECT_THAT</code> a powerful, extensible assertion.</p>
<p><em>The idea for this assertion was borrowed from Joe Walnes' Hamcrest project, which adds <code>assertThat()</code> to JUnit.</em></p>
<h1><a class="anchor" id="boolean"></a>
Boolean Conditions</h1>
<p>The following assertions test Boolean conditions.</p>
<h2><a class="anchor" id="EXPECT_TRUE"></a>
EXPECT_TRUE</h2>
<p><code>EXPECT_TRUE(</code>*<code>condition</code>*<code>)</code> \ <code>ASSERT_TRUE(</code>*<code>condition</code>*<code>)</code></p>
<p>Verifies that <em><code>condition</code></em> is true.</p>
<h2><a class="anchor" id="EXPECT_FALSE"></a>
EXPECT_FALSE</h2>
<p><code>EXPECT_FALSE(</code>*<code>condition</code>*<code>)</code> \ <code>ASSERT_FALSE(</code>*<code>condition</code>*<code>)</code></p>
<p>Verifies that <em><code>condition</code></em> is false.</p>
<h1><a class="anchor" id="binary-comparison"></a>
Binary Comparison</h1>
<p>The following assertions compare two values. The value arguments must be comparable by the assertion's comparison operator, otherwise a compiler error will result.</p>
<p>If an argument supports the <code>&lt;&lt;</code> operator, it will be called to print the argument when the assertion fails. Otherwise, GoogleTest will attempt to print them in the best way it can—see <a href="../advanced.md#teaching-googletest-how-to-print-your-values">Teaching GoogleTest How to Print Your Values</a>.</p>
<p>Arguments are always evaluated exactly once, so it's OK for the arguments to have side effects. However, the argument evaluation order is undefined and programs should not depend on any particular argument evaluation order.</p>
<p>These assertions work with both narrow and wide string objects (<code>string</code> and <code>wstring</code>).</p>
<p>See also the <a class="el" href="#floating-point">Floating-Point Comparison</a> assertions to compare floating-point numbers and avoid problems caused by rounding.</p>
<h2><a class="anchor" id="EXPECT_EQ"></a>
EXPECT_EQ</h2>
<p><code>EXPECT_EQ(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_EQ(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that <em><code>val1</code></em><code>==</code>*<code>val2</code>*.</p>
<p>Does pointer equality on pointers. If used on two C strings, it tests if they are in the same memory location, not if they have the same value. Use <a class="el" href="#EXPECT_STREQ">`EXPECT_STREQ`</a> to compare C strings (e.g. <code>const char*</code>) by value.</p>
<p>When comparing a pointer to <code>NULL</code>, use <code>EXPECT_EQ(</code>*<code>ptr</code>*<code>, nullptr)</code> instead of <code>EXPECT_EQ(</code>*<code>ptr</code>*<code>, NULL)</code>.</p>
<h2><a class="anchor" id="EXPECT_NE"></a>
EXPECT_NE</h2>
<p><code>EXPECT_NE(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_NE(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that <em><code>val1</code></em><code>!=</code>*<code>val2</code>*.</p>
<p>Does pointer equality on pointers. If used on two C strings, it tests if they are in different memory locations, not if they have different values. Use <a class="el" href="#EXPECT_STRNE">`EXPECT_STRNE`</a> to compare C strings (e.g. <code>const char*</code>) by value.</p>
<p>When comparing a pointer to <code>NULL</code>, use <code>EXPECT_NE(</code>*<code>ptr</code>*<code>, nullptr)</code> instead of <code>EXPECT_NE(</code>*<code>ptr</code>*<code>, NULL)</code>.</p>
<h2><a class="anchor" id="EXPECT_LT"></a>
EXPECT_LT</h2>
<p><code>EXPECT_LT(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_LT(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that <em><code>val1</code></em><code>&lt;</code>*<code>val2</code>*.</p>
<h2><a class="anchor" id="EXPECT_LE"></a>
EXPECT_LE</h2>
<p><code>EXPECT_LE(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_LE(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that <em><code>val1</code></em><code>&lt;=</code>*<code>val2</code>*.</p>
<h2><a class="anchor" id="EXPECT_GT"></a>
EXPECT_GT</h2>
<p><code>EXPECT_GT(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_GT(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that <em><code>val1</code></em><code>&gt;</code>*<code>val2</code>*.</p>
<h2><a class="anchor" id="EXPECT_GE"></a>
EXPECT_GE</h2>
<p><code>EXPECT_GE(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_GE(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that <em><code>val1</code></em><code>&gt;=</code>*<code>val2</code>*.</p>
<h1><a class="anchor" id="c-strings"></a>
String Comparison</h1>
<p>The following assertions compare two <b>C strings</b>. To compare two <code>string</code> objects, use <a class="el" href="#EXPECT_EQ">`EXPECT_EQ`</a> or <a class="el" href="#EXPECT_NE">`EXPECT_NE`</a> instead.</p>
<p>These assertions also accept wide C strings (<code>wchar_t*</code>). If a comparison of two wide strings fails, their values will be printed as UTF-8 narrow strings.</p>
<p>To compare a C string with <code>NULL</code>, use <code>EXPECT_EQ(</code>*<code>c_string</code>*<code>, nullptr)</code> or <code>EXPECT_NE(</code>*<code>c_string</code>*<code>, nullptr)</code>.</p>
<h2><a class="anchor" id="EXPECT_STREQ"></a>
EXPECT_STREQ</h2>
<p><code>EXPECT_STREQ(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code> \ <code>ASSERT_STREQ(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code></p>
<p>Verifies that the two C strings <em><code>str1</code></em> and <em><code>str2</code></em> have the same contents.</p>
<h2><a class="anchor" id="EXPECT_STRNE"></a>
EXPECT_STRNE</h2>
<p><code>EXPECT_STRNE(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code> \ <code>ASSERT_STRNE(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code></p>
<p>Verifies that the two C strings <em><code>str1</code></em> and <em><code>str2</code></em> have different contents.</p>
<h2><a class="anchor" id="EXPECT_STRCASEEQ"></a>
EXPECT_STRCASEEQ</h2>
<p><code>EXPECT_STRCASEEQ(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code> \ <code>ASSERT_STRCASEEQ(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code></p>
<p>Verifies that the two C strings <em><code>str1</code></em> and <em><code>str2</code></em> have the same contents, ignoring case.</p>
<h2><a class="anchor" id="EXPECT_STRCASENE"></a>
EXPECT_STRCASENE</h2>
<p><code>EXPECT_STRCASENE(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code> \ <code>ASSERT_STRCASENE(</code>*<code>str1</code>*<code>,</code>*<code>str2</code>*<code>)</code></p>
<p>Verifies that the two C strings <em><code>str1</code></em> and <em><code>str2</code></em> have different contents, ignoring case.</p>
<h1><a class="anchor" id="floating-point"></a>
Floating-Point Comparison</h1>
<p>The following assertions compare two floating-point values.</p>
<p>Due to rounding errors, it is very unlikely that two floating-point values will match exactly, so <code>EXPECT_EQ</code> is not suitable. In general, for floating-point comparison to make sense, the user needs to carefully choose the error bound.</p>
<p>GoogleTest also provides assertions that use a default error bound based on Units in the Last Place (ULPs). To learn more about ULPs, see the article <a href="https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/">Comparing Floating Point Numbers</a>.</p>
<h2><a class="anchor" id="EXPECT_FLOAT_EQ"></a>
EXPECT_FLOAT_EQ</h2>
<p><code>EXPECT_FLOAT_EQ(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_FLOAT_EQ(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that the two <code>float</code> values <em><code>val1</code></em> and <em><code>val2</code></em> are approximately equal, to within 4 ULPs from each other.</p>
<h2><a class="anchor" id="EXPECT_DOUBLE_EQ"></a>
EXPECT_DOUBLE_EQ</h2>
<p><code>EXPECT_DOUBLE_EQ(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_DOUBLE_EQ(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code></p>
<p>Verifies that the two <code>double</code> values <em><code>val1</code></em> and <em><code>val2</code></em> are approximately equal, to within 4 ULPs from each other.</p>
<h2><a class="anchor" id="EXPECT_NEAR"></a>
EXPECT_NEAR</h2>
<p><code>EXPECT_NEAR(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>abs_error</code>*<code>)</code> \ <code>ASSERT_NEAR(</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>abs_error</code>*<code>)</code></p>
<p>Verifies that the difference between <em><code>val1</code></em> and <em><code>val2</code></em> does not exceed the absolute error bound <em><code>abs_error</code></em>.</p>
<h1><a class="anchor" id="exceptions"></a>
Exception Assertions</h1>
<p>The following assertions verify that a piece of code throws, or does not throw, an exception. Usage requires exceptions to be enabled in the build environment.</p>
<p>Note that the piece of code under test can be a compound statement, for example:</p>
<div class="fragment"><div class="line">EXPECT_NO_THROW({</div>
<div class="line">  <span class="keywordtype">int</span> n = 5;</div>
<div class="line">  DoSomething(&amp;n);</div>
<div class="line">});</div>
</div><!-- fragment --><h2><a class="anchor" id="EXPECT_THROW"></a>
EXPECT_THROW</h2>
<p><code>EXPECT_THROW(</code>*<code>statement</code>*<code>,</code>*<code>exception_type</code>*<code>)</code> \ <code>ASSERT_THROW(</code>*<code>statement</code>*<code>,</code>*<code>exception_type</code>*<code>)</code></p>
<p>Verifies that <em><code>statement</code></em> throws an exception of type <em><code>exception_type</code></em>.</p>
<h2><a class="anchor" id="EXPECT_ANY_THROW"></a>
EXPECT_ANY_THROW</h2>
<p><code>EXPECT_ANY_THROW(</code>*<code>statement</code>*<code>)</code> \ <code>ASSERT_ANY_THROW(</code>*<code>statement</code>*<code>)</code></p>
<p>Verifies that <em><code>statement</code></em> throws an exception of any type.</p>
<h2><a class="anchor" id="EXPECT_NO_THROW"></a>
EXPECT_NO_THROW</h2>
<p><code>EXPECT_NO_THROW(</code>*<code>statement</code>*<code>)</code> \ <code>ASSERT_NO_THROW(</code>*<code>statement</code>*<code>)</code></p>
<p>Verifies that <em><code>statement</code></em> does not throw any exception.</p>
<h1><a class="anchor" id="predicates"></a>
Predicate Assertions</h1>
<p>The following assertions enable more complex predicates to be verified while printing a more clear failure message than if <code>EXPECT_TRUE</code> were used alone.</p>
<h2><a class="anchor" id="EXPECT_PRED"></a>
EXPECT_PRED*</h2>
<p><code>EXPECT_PRED1(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>)</code> \ <code>EXPECT_PRED2(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>EXPECT_PRED3(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>)</code> \ <code>EXPECT_PRED4(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>)</code> \ <code>EXPECT_PRED5(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>,</code>*<code>val5</code>*<code>)</code></p>
<p><code>ASSERT_PRED1(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>)</code> \ <code>ASSERT_PRED2(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_PRED3(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>)</code> \ <code>ASSERT_PRED4(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>)</code> \ <code>ASSERT_PRED5(</code>*<code>pred</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>,</code>*<code>val5</code>*<code>)</code></p>
<p>Verifies that the predicate <em><code>pred</code></em> returns <code>true</code> when passed the given values as arguments.</p>
<p>The parameter <em><code>pred</code></em> is a function or functor that accepts as many arguments as the corresponding macro accepts values. If <em><code>pred</code></em> returns <code>true</code> for the given arguments, the assertion succeeds, otherwise the assertion fails.</p>
<p>When the assertion fails, it prints the value of each argument. Arguments are always evaluated exactly once.</p>
<p>As an example, see the following code:</p>
<div class="fragment"><div class="line"><span class="comment">// Returns true if m and n have no common divisors except 1.</span></div>
<div class="line"><span class="keywordtype">bool</span> MutuallyPrime(<span class="keywordtype">int</span> m, <span class="keywordtype">int</span> n) { ... }</div>
<div class="line">...</div>
<div class="line">const <span class="keywordtype">int</span> a = 3;</div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> b = 4;</div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> c = 10;</div>
<div class="line">...</div>
<div class="line">EXPECT_PRED2(MutuallyPrime, a, b);  <span class="comment">// Succeeds</span></div>
<div class="line">EXPECT_PRED2(MutuallyPrime, b, c);  <span class="comment">// Fails</span></div>
</div><!-- fragment --><p>In the above example, the first assertion succeeds, and the second fails with the following message:</p>
<div class="fragment"><div class="line">MutuallyPrime(b, c) is false, where</div>
<div class="line">b is 4</div>
<div class="line">c is 10</div>
</div><!-- fragment --><p>Note that if the given predicate is an overloaded function or a function template, the assertion macro might not be able to determine which version to use, and it might be necessary to explicitly specify the type of the function. For example, for a Boolean function <code>IsPositive()</code> overloaded to take either a single <code>int</code> or <code>double</code> argument, it would be necessary to write one of the following:</p>
<div class="fragment"><div class="line">EXPECT_PRED1(<span class="keyword">static_cast&lt;</span><span class="keywordtype">bool</span> (*)(<span class="keywordtype">int</span>)<span class="keyword">&gt;</span>(IsPositive), 5);</div>
<div class="line">EXPECT_PRED1(<span class="keyword">static_cast&lt;</span><span class="keywordtype">bool</span> (*)(<span class="keywordtype">double</span>)<span class="keyword">&gt;</span>(IsPositive), 3.14);</div>
</div><!-- fragment --><p>Writing simply <code>EXPECT_PRED1(IsPositive, 5);</code> would result in a compiler error. Similarly, to use a template function, specify the template arguments:</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div>
<div class="line"><span class="keywordtype">bool</span> IsNegative(T x) {</div>
<div class="line">  <span class="keywordflow">return</span> x &lt; 0;</div>
<div class="line">}</div>
<div class="line">...</div>
<div class="line">EXPECT_PRED1(IsNegative&lt;int&gt;, -5);  <span class="comment">// Must specify type for IsNegative</span></div>
</div><!-- fragment --><p>If a template has multiple parameters, wrap the predicate in parentheses so the macro arguments are parsed correctly:</p>
<div class="fragment"><div class="line">ASSERT_PRED2((MyPredicate&lt;int, int&gt;), 5, 0);</div>
</div><!-- fragment --><h2><a class="anchor" id="EXPECT_PRED_FORMAT"></a>
EXPECT_PRED_FORMAT*</h2>
<p><code>EXPECT_PRED_FORMAT1(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>)</code> \ <code>EXPECT_PRED_FORMAT2(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>EXPECT_PRED_FORMAT3(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>)</code> \ <code>EXPECT_PRED_FORMAT4(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>)</code> \ <code>EXPECT_PRED_FORMAT5(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>,</code>*<code>val5</code>*<code>)</code></p>
<p><code>ASSERT_PRED_FORMAT1(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>)</code> \ <code>ASSERT_PRED_FORMAT2(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>)</code> \ <code>ASSERT_PRED_FORMAT3(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>)</code> \ <code>ASSERT_PRED_FORMAT4(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>)</code> \ <code>ASSERT_PRED_FORMAT5(</code>*<code>pred_formatter</code>*<code>,</code>*<code>val1</code>*<code>,</code>*<code>val2</code>*<code>,</code>*<code>val3</code>*<code>,</code>*<code>val4</code>*<code>,</code>*<code>val5</code>*<code>)</code></p>
<p>Verifies that the predicate <em><code>pred_formatter</code></em> succeeds when passed the given values as arguments.</p>
<p>The parameter <em><code>pred_formatter</code></em> is a <em>predicate-formatter</em>, which is a function or functor with the signature:</p>
<div class="fragment"><div class="line">testing::AssertionResult PredicateFormatter(<span class="keyword">const</span> <span class="keywordtype">char</span>* expr1,</div>
<div class="line">                                            <span class="keyword">const</span> <span class="keywordtype">char</span>* expr2,</div>
<div class="line">                                            ...</div>
<div class="line">                                            <span class="keyword">const</span> <span class="keywordtype">char</span>* exprn,</div>
<div class="line">                                            T1 val1,</div>
<div class="line">                                            T2 val2,</div>
<div class="line">                                            ...</div>
<div class="line">                                            Tn valn);</div>
</div><!-- fragment --><p>where <em><code>val1</code></em>, <em><code>val2</code></em>, ..., <em><code>valn</code></em> are the values of the predicate arguments, and <em><code>expr1</code></em>, <em><code>expr2</code></em>, ..., <em><code>exprn</code></em> are the corresponding expressions as they appear in the source code. The types <code>T1</code>, <code>T2</code>, ..., <code>Tn</code> can be either value types or reference types; if an argument has type <code>T</code>, it can be declared as either <code>T</code> or <code>const T&amp;</code>, whichever is appropriate. For more about the return type <code>testing::AssertionResult</code>, see <a href="../advanced.md#using-a-function-that-returns-an-assertionresult">Using a Function That Returns an AssertionResult</a>.</p>
<p>As an example, see the following code:</p>
<div class="fragment"><div class="line"><span class="comment">// Returns the smallest prime common divisor of m and n,</span></div>
<div class="line"><span class="comment">// or 1 when m and n are mutually prime.</span></div>
<div class="line"><span class="keywordtype">int</span> SmallestPrimeCommonDivisor(<span class="keywordtype">int</span> m, <span class="keywordtype">int</span> n) { ... }</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Returns true if m and n have no common divisors except 1.</span></div>
<div class="line"><span class="keywordtype">bool</span> MutuallyPrime(<span class="keywordtype">int</span> m, <span class="keywordtype">int</span> n) { ... }</div>
<div class="line"> </div>
<div class="line"><span class="comment">// A predicate-formatter for asserting that two integers are mutually prime.</span></div>
<div class="line">testing::AssertionResult AssertMutuallyPrime(<span class="keyword">const</span> <span class="keywordtype">char</span>* m_expr,</div>
<div class="line">                                             <span class="keyword">const</span> <span class="keywordtype">char</span>* n_expr,</div>
<div class="line">                                             <span class="keywordtype">int</span> m,</div>
<div class="line">                                             <span class="keywordtype">int</span> n) {</div>
<div class="line">  <span class="keywordflow">if</span> (MutuallyPrime(m, n)) <span class="keywordflow">return</span> testing::AssertionSuccess();</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">return</span> testing::AssertionFailure() &lt;&lt; m_expr &lt;&lt; <span class="stringliteral">&quot; and &quot;</span> &lt;&lt; n_expr</div>
<div class="line">      &lt;&lt; <span class="stringliteral">&quot; (&quot;</span> &lt;&lt; m &lt;&lt; <span class="stringliteral">&quot; and &quot;</span> &lt;&lt; n &lt;&lt; <span class="stringliteral">&quot;) are not mutually prime, &quot;</span></div>
<div class="line">      &lt;&lt; <span class="stringliteral">&quot;as they have a common divisor &quot;</span> &lt;&lt; SmallestPrimeCommonDivisor(m, n);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">...</div>
<div class="line">const <span class="keywordtype">int</span> a = 3;</div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> b = 4;</div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> c = 10;</div>
<div class="line">...</div>
<div class="line">EXPECT_PRED_FORMAT2(AssertMutuallyPrime, a, b);  <span class="comment">// Succeeds</span></div>
<div class="line">EXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c);  <span class="comment">// Fails</span></div>
</div><!-- fragment --><p>In the above example, the final assertion fails and the predicate-formatter produces the following failure message:</p>
<div class="fragment"><div class="line">b and c (4 and 10) are not mutually prime, as they have a common divisor 2</div>
</div><!-- fragment --><h1><a class="anchor" id="HRESULT"></a>
Windows HRESULT Assertions</h1>
<p>The following assertions test for <code>HRESULT</code> success or failure. For example:</p>
<div class="fragment"><div class="line">CComPtr&lt;IShellDispatch2&gt; shell;</div>
<div class="line">ASSERT_HRESULT_SUCCEEDED(shell.CoCreateInstance(L<span class="stringliteral">&quot;Shell.Application&quot;</span>));</div>
<div class="line">CComVariant empty;</div>
<div class="line">ASSERT_HRESULT_SUCCEEDED(shell-&gt;ShellExecute(CComBSTR(url), empty, empty, empty, empty));</div>
</div><!-- fragment --><p>The generated output contains the human-readable error message associated with the returned <code>HRESULT</code> code.</p>
<h2><a class="anchor" id="EXPECT_HRESULT_SUCCEEDED"></a>
EXPECT_HRESULT_SUCCEEDED</h2>
<p><code>EXPECT_HRESULT_SUCCEEDED(</code>*<code>expression</code>*<code>)</code> \ <code>ASSERT_HRESULT_SUCCEEDED(</code>*<code>expression</code>*<code>)</code></p>
<p>Verifies that <em><code>expression</code></em> is a success <code>HRESULT</code>.</p>
<h2><a class="anchor" id="EXPECT_HRESULT_FAILED"></a>
EXPECT_HRESULT_FAILED</h2>
<p><code>EXPECT_HRESULT_FAILED(</code>*<code>expression</code>*<code>)</code> \ <code>EXPECT_HRESULT_FAILED(</code>*<code>expression</code>*<code>)</code></p>
<p>Verifies that <em><code>expression</code></em> is a failure <code>HRESULT</code>.</p>
<h1><a class="anchor" id="death"></a>
Death Assertions</h1>
<p>The following assertions verify that a piece of code causes the process to terminate. For context, see <a href="../advanced.md#death-tests">Death Tests</a>.</p>
<p>These assertions spawn a new process and execute the code under test in that process. How that happens depends on the platform and the variable <code>::testing::GTEST_FLAG(death_test_style)</code>, which is initialized from the command-line flag <code>--gtest_death_test_style</code>.</p>
<ul>
<li>On POSIX systems, <code>fork()</code> (or <code>clone()</code> on Linux) is used to spawn the child, after which:<ul>
<li>If the variable's value is <code>"fast"</code>, the death test statement is immediately executed.</li>
<li>If the variable's value is <code>"threadsafe"</code>, the child process re-executes the unit test binary just as it was originally invoked, but with some extra flags to cause just the single death test under consideration to be run.</li>
</ul>
</li>
<li>On Windows, the child is spawned using the <code>CreateProcess()</code> API, and re-executes the binary to cause just the single death test under consideration to be run - much like the <code>"threadsafe"</code> mode on POSIX.</li>
</ul>
<p>Other values for the variable are illegal and will cause the death test to fail. Currently, the flag's default value is <b><code>"fast"</code></b>.</p>
<p>If the death test statement runs to completion without dying, the child process will nonetheless terminate, and the assertion fails.</p>
<p>Note that the piece of code under test can be a compound statement, for example:</p>
<div class="fragment"><div class="line">EXPECT_DEATH({</div>
<div class="line">  <span class="keywordtype">int</span> n = 5;</div>
<div class="line">  DoSomething(&amp;n);</div>
<div class="line">}, <span class="stringliteral">&quot;Error on line .* of DoSomething()&quot;</span>);</div>
</div><!-- fragment --><h2><a class="anchor" id="EXPECT_DEATH"></a>
EXPECT_DEATH</h2>
<p><code>EXPECT_DEATH(</code>*<code>statement</code>*<code>,</code>*<code>matcher</code>*<code>)</code> \ <code>ASSERT_DEATH(</code>*<code>statement</code>*<code>,</code>*<code>matcher</code>*<code>)</code></p>
<p>Verifies that <em><code>statement</code></em> causes the process to terminate with a nonzero exit status and produces <code>stderr</code> output that matches <em><code>matcher</code></em>.</p>
<p>The parameter <em><code>matcher</code></em> is either a <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2matchers.html">matcher</a> for a <code>const std::string&amp;</code>, or a regular expression (see <a href="../advanced.md#regular-expression-syntax">Regular Expression Syntax</a>)—a bare string <em><code>s</code></em> (with no matcher) is treated as <a href="matchers.md#string-matchers"><code>ContainsRegex(s)</code></a>, <b>not</b> <a href="matchers.md#generic-comparison"><code>Eq(s)</code></a>.</p>
<p>For example, the following code verifies that calling <code>DoSomething(42)</code> causes the process to die with an error message that contains the text <code>My error</code>:</p>
<div class="fragment"><div class="line">EXPECT_DEATH(DoSomething(42), <span class="stringliteral">&quot;My error&quot;</span>);</div>
</div><!-- fragment --><h2><a class="anchor" id="EXPECT_DEATH_IF_SUPPORTED"></a>
EXPECT_DEATH_IF_SUPPORTED</h2>
<p><code>EXPECT_DEATH_IF_SUPPORTED(</code>*<code>statement</code>*<code>,</code>*<code>matcher</code>*<code>)</code> \ <code>ASSERT_DEATH_IF_SUPPORTED(</code>*<code>statement</code>*<code>,</code>*<code>matcher</code>*<code>)</code></p>
<p>If death tests are supported, behaves the same as <a class="el" href="#EXPECT_DEATH">`EXPECT_DEATH`</a>. Otherwise, verifies nothing.</p>
<h2><a class="anchor" id="EXPECT_DEBUG_DEATH"></a>
EXPECT_DEBUG_DEATH</h2>
<p><code>EXPECT_DEBUG_DEATH(</code>*<code>statement</code>*<code>,</code>*<code>matcher</code>*<code>)</code> \ <code>ASSERT_DEBUG_DEATH(</code>*<code>statement</code>*<code>,</code>*<code>matcher</code>*<code>)</code></p>
<p>In debug mode, behaves the same as <a class="el" href="#EXPECT_DEATH">`EXPECT_DEATH`</a>. When not in debug mode (i.e. <code>NDEBUG</code> is defined), just executes <em><code>statement</code></em>.</p>
<h2><a class="anchor" id="EXPECT_EXIT"></a>
EXPECT_EXIT</h2>
<p><code>EXPECT_EXIT(</code>*<code>statement</code>*<code>,</code>*<code>predicate</code>*<code>,</code>*<code>matcher</code>*<code>)</code> \ <code>ASSERT_EXIT(</code>*<code>statement</code>*<code>,</code>*<code>predicate</code>*<code>,</code>*<code>matcher</code>*<code>)</code></p>
<p>Verifies that <em><code>statement</code></em> causes the process to terminate with an exit status that satisfies <em><code>predicate</code></em>, and produces <code>stderr</code> output that matches <em><code>matcher</code></em>.</p>
<p>The parameter <em><code>predicate</code></em> is a function or functor that accepts an <code>int</code> exit status and returns a <code>bool</code>. GoogleTest provides two predicates to handle common cases:</p>
<div class="fragment"><div class="line"><span class="comment">// Returns true if the program exited normally with the given exit status code.</span></div>
<div class="line">::testing::ExitedWithCode(exit_code);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Returns true if the program was killed by the given signal.</span></div>
<div class="line"><span class="comment">// Not available on Windows.</span></div>
<div class="line">::testing::KilledBySignal(signal_number);</div>
</div><!-- fragment --><p>The parameter <em><code>matcher</code></em> is either a <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2matchers.html">matcher</a> for a <code>const std::string&amp;</code>, or a regular expression (see <a href="../advanced.md#regular-expression-syntax">Regular Expression Syntax</a>)—a bare string <em><code>s</code></em> (with no matcher) is treated as <a href="matchers.md#string-matchers"><code>ContainsRegex(s)</code></a>, <b>not</b> <a href="matchers.md#generic-comparison"><code>Eq(s)</code></a>.</p>
<p>For example, the following code verifies that calling <code>NormalExit()</code> causes the process to print a message containing the text <code>Success</code> to <code>stderr</code> and exit with exit status code 0:</p>
<div class="fragment"><div class="line">EXPECT_EXIT(NormalExit(), testing::ExitedWithCode(0), <span class="stringliteral">&quot;Success&quot;</span>);</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
  </ul>
</div>
</body>
</html>
